package com.wangqingbo.myspringbootproject.controller;

import com.wangqingbo.myspringbootproject.dao.StudentDao;
import com.wangqingbo.myspringbootproject.entity.ProjectInfo;
import com.wangqingbo.myspringbootproject.entity.StudentInfo;
import com.wangqingbo.myspringbootproject.jdbc.MyDataSource;
import com.wangqingbo.myspringbootproject.service.ProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class MyController {

    // 注入MyDataSource的bean
    @Autowired
    private MyDataSource ds;

    // 注入student的bean
    @Autowired
    private StudentInfo si;
    // 注入StudentDao
    @Autowired
    private StudentDao studentDao;
    /**
     * 测试springMVC的访问
     * @ResponseBody 注解，返回json格式的数据
     * @return
     */
    @RequestMapping("/test")
    @ResponseBody
    public String testController()
    {
        return "My First SpringBoot Project";
    }

    /**
     * method = RequestMethod.POST 制定方法的访问方式
     * @return
     */
    @RequestMapping(value = "/getDb",method = RequestMethod.GET)
    @ResponseBody
    public MyDataSource getMyDataSource()
    {
       return ds;
    }

    /**
     * 获取student信息
     * @return
     */
    @RequestMapping("/getSt")
    @ResponseBody
    public StudentInfo getStudent()
    {
       studentDao.saveStudent();
      return si;
    }

    /**
     * 从后台取数据到jsp页面
     * @return
     */
    @RequestMapping("/getStuJsp")
    public ModelAndView getStudentForJsp()
    {
        ModelAndView mv = new ModelAndView();
        // 绑定学生信息的对象
        mv.addObject("student",si);
        // 返回页面，配置了视图解析器，这里直接写文件名
        mv.setViewName("index");
        return mv;
    }

    /**
     * 访问国际化资源文件
     * @return
     */
    @RequestMapping("/getI18n")
    public ModelAndView getI18n()
    {
        ModelAndView mv = new ModelAndView();
        // 返回页面，配置了视图解析器，这里直接写文件名
        mv.setViewName("i18n");
        return mv;
    }


}
